import { message } from 'antd';
import * as analysisServices from '../services/analysis';


export default {
  namespace: 'analysis',
  state: {
    versionPlan: [],
    versionAnalysis: [],
    versionAnalysisDetail: [],
    sprintAnalysis: [],
    sprintAnalysisDetail: [],
    paginal: {
      current: 1,
      totalRecord: 0,
    },
  },
  reducers: { // 修改state
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *fetchVersionPlan({ payload: { params } }, { call, put }) {
      const { data } = yield call(analysisServices.fetchVersionPlan, params);// 接口调用
      yield put({
        type: 'save',
        payload: {
          versionPlan: data.data.objects || [], // 逻辑表达式，前面为空则用后面的值
          paginal: {
            current: params.current,
            totalRecord: data.data.paginator.total,
          },
        },
      });
    },
    *addPlan({ payload: { versionparam, params, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.addVersionPlan, versionparam);
      const errorMes = data.message || '新增失败';
      if (data.status === 200) {
        message.success('新增成功！');
        if (cb) cb();
        yield put({
          type: 'fetchVersionPlan',
          payload: {
            params,
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *updatePlan({ payload: { versionparam, params, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.updateVersionPlan, versionparam);
      const errorMes = data.message || '修改失败';
      if (data.status === 200) {
        message.success('修改成功！');
        if (cb) cb();
        yield put({
          type: 'fetchVersionPlan',
          payload: {
            params,
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    * deletePlan({ payload: { recordParams, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.deleteVersionPlan, recordParams);
      const errorMes = data.message || '删除失败';
      if (data.status === 200) {
        message.success('删除成功！');
        if (cb) cb(); // 关闭弹窗
        yield put({
          type: 'fetchVersionPlan',
          payload: {
            params: {
              fixversion_id: '', // record对象中的id字段
              project_name: '',
            },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *fetchVersionAnalysis({ payload: { params, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.getVersionAnalysisResult, params);
      yield put({
        type: 'save',
        payload: {
          versionAnalysis: data.data || [],
        },
      });
    },
    *fetchVersionAnalysisDetail({ payload: { params, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.getVersionAnalysisDetail, params);
      yield put({
        type: 'save',
        payload: {
          versionAnalysisDetail: data.data || [],
        },
      });
    },
    *fetchSprintAnalysis({ payload: { params, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.getSprintAnalysis, params);
      yield put({
        type: 'save',
        payload: {
          sprintAnalysis: data.data || [],
        },
      });
    },
    *fetchSprintAnalysisDetail({ payload: { params, cb } }, { call, put }) {
      const { data } = yield call(analysisServices.getSprintAnaDetail, params);
      yield put({
        type: 'save',
        payload: {
          sprintAnalysisDetail: data.data || [],
        },
      });
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(() => {
        // dispatch({ type: 'fetchVersionPlan' });
      });
    },
  },
};
